package com.player.rest.resource;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.json.JSONException;

import com.player.dao.DAOLocator;
import com.player.entity.Game;
import com.player.entity.Player;
import com.player.rest.util.ResponseConverter;

@Path("search")
public class SearchResource {

	@GET
	@Path("players/playername/{playername}")
	@Produces(MediaType.APPLICATION_JSON)
	public String findPlayersByPlayername(@PathParam("playername") String playername) throws JSONException {
		if (playername == null || "".equals(playername)) {
			return ResponseConverter.toJSONPlayer(new ArrayList<Player>());
		}

		List<Player> players = DAOLocator.getPlayerDAO().findByPlayername(playername);
		return ResponseConverter.toJSONPlayer(players);
	}

	@GET
	@Path("games/creator/{creatorId}")
	@Produces(MediaType.APPLICATION_JSON)
	public String findGamesByCreator(@PathParam("creatorId") Long creatorId) throws JSONException {
		List<Game> games = DAOLocator.getGameDAO().getByCreator(creatorId);
		return ResponseConverter.toJSONGame(games);
	}

	@GET
	@Path("games/unconfirmed/{playerId}")
	@Produces(MediaType.APPLICATION_JSON)
	public String findUnconfirmedGames(@PathParam("playerId") Long playerId) throws JSONException {
		// FIXME auth
		List<Game> games = DAOLocator.getGameDAO().getGamesToBeConfirmed(playerId);
		return ResponseConverter.toJSONGame(games);
	}

}
